package com.cmc.cloud.cmclink.doc.rpcservice.impl;

import com.cmc.cloud.cmclink.bdt.api.base.PortApi;
import com.cmc.cloud.cmclink.bdt.api.base.dto.req.PortListByCodeReqDTO;
import com.cmc.cloud.cmclink.bdt.api.base.dto.resp.PortListByCodeRespDTO;
import com.cmc.cloud.cmclink.doc.rpcservice.PortService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lzp
 */
@Slf4j
@Service
public class PortServiceImpl implements PortService {

    @Resource
    private PortApi portApi;

    /**
     * 通过港口编码获取港口名称
     *
     * @param portListByCodeReqDto
     * @return
     */
    @Override
    public List<PortListByCodeRespDTO> listByCodeList(PortListByCodeReqDTO portListByCodeReqDto) {
        List<PortListByCodeRespDTO> result = new ArrayList<>(16);
        try {
            result = portApi.listByCodeList(portListByCodeReqDto).getCheckedData();
            if (CollectionUtils.isNotEmpty(result)) {
                return result.stream().filter(item -> item.getEnabled()).collect(Collectors.toList());
            } else {
                return Collections.emptyList();
            }
        } catch (Exception e) {
            log.info(e.getMessage());
            return Collections.emptyList();
        }
    }
}
